var publicKey = 'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC57mdBto9AbJRXqk6OvuBalxSTpVxBUzq+4Eq11XLsOMYAPL5dew3pLn+l3tTwEgnMYHd1cMI+KgFkU+MJYuOSix/tkUNPuR6f1A0sKDfhretJqP4t0TQ3DW0RA1F1SLGf3BKEYU8KoAGueNgn1xaNlltP5pK+OYKIPfYWX8zv9wIDAQAB';

/**
 * URL encoder
 * @param str
 * @returns {string}
 * @constructor
 */
function URLencode(str) {
    return escape(str).replace(/\+/g, '%2B').replace(/\"/g,'%22').replace(/\'/g, '%27').replace(/\//g,'%2F');
}

/**
 * 用来随机生成指定字符长度（个数）的字符串值。这里主要用于128 bit的加密码。
 * @param number
 * @returns {string}
 */

function makeKey(number) {
    var result           = '';
    var characters       = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
    var charactersLength = characters.length;
    for ( var i = 0; i < number; i++ ) {
        result += characters.charAt(Math.floor(Math.random() * charactersLength));
    }
    return result;
}

/**
 * Cookie方法
 * @param cname
 * @param cvalue
 * @param exdays
 */
function setCookie(cname,cvalue,exdays) {
    var d = new Date();
    d.setTime(d.getTime()+(exdays*24*60*60*1000));
    var expires = "expires="+d.toGMTString();
    document.cookie = cname + "=" + cvalue + "; " + "path=/;" + expires;
}

/**
 * 得到某个cookie字段
 * @param cname
 * @returns {string}
 */
function getCookie(cname) {
    var name = cname + "=";
    var ca = document.cookie.split(';');
    for(var i=0; i<ca.length; i++)
    {
        var c = ca[i].trim();
        if (c.indexOf(name)==0) return c.substring(name.length,c.length);
    }
    return "";
}

/**
 * 查看cookie是否存在
 */
function checkCookie() {
    var username=getCookie("username");
    if (username!="")
    {
        alert("Welcome again " + username);
    }
    else
    {
        username = prompt("Please enter your name:","");
        if (username!="" && username!=null)
        {
            setCookie("username",username,365);
        }
    }
}

function deleteCookie(cname) {
    document.cookie = cname + "= ;"  + "path=/;"  + "expires = Thu, 01 Jan 1970 00:00:00 GMT";
}

/**
 * 点击是否在某个element之内
 * @param e
 * @param className
 * @returns {HTMLElement|(() => (Node | null))|ActiveX.IXMLDOMNode|(Node & ParentNode)|parentNode|{classList}|boolean|EventTarget|*}
 */
function clickInsideElement(e, className ) {
    var el = e.srcElement || e.target;

    if (el.classList.contains(className)) {
        return el;
    }
    else {
        while (el = el.parentNode) {
            if (el.classList && el.classList.contains(className)) {
                return el;
            }
        }
    }

    return false;
}

/**
 * 測試字符串里所有的數字是不是都是數字
 * @param str
 * @returns {boolean}
 */
function isAllDigit(str) {
    return /^\d+$/.test(str);
}

/**
 * 登出
 */
function logout() {
    deleteCookie("token");
    window.location.reload(true);
}


/**
 * 显示一个提示用户登录的对话框
 */
function showDialogToAskLogin(action) {
    Swal.fire({
        title: '<strong>请登录</strong>',
        icon: 'info',
        html:
            '您必须先<b>登录</b>才能' + action +
            '<a href="/login" class="ml-2 mr-2">点击这里登录</a> ',
        showCloseButton: true,
        showCancelButton: true,
        focusConfirm: true,
        cancelButtonText:
            '暂时不要',
        confirmButtonText:
            '去登录',
        confirmButtonAriaLabel: 'Thumbs up, great!',
        cancelButtonAriaLabel: 'Thumbs down'
    }).then((result) => {
        if (result.value) {
            window.location.href = document.location.origin + '/login';
        }
    });
}

/**
 * 显示一个提示用户跳转到某页的对话框
 * @param page
 */
function showDialogToJumpToPage(title, pagePath, confirmText) {
    Swal.fire({
        title: '<strong>' + title + '</strong>',
        icon: 'info',
        focusConfirm: true,
        confirmButtonText: confirmText,
        confirmButtonAriaLabel: 'Thumbs up, great!',
        cancelButtonAriaLabel: 'Thumbs down'
    }).then((result) => {
        if (result.value) {
            window.location.href = document.location.origin + pagePath;
        }
    });
}

/**
 * 显示一个信息对话框
 */
function showDialog(info) {
    Swal.fire({
        title: '<strong>' + info + '</strong>',
        icon: 'info',
        focusConfirm: true,
        confirmButtonText: "确认",
    });
}

/**
 * 显示自动关闭的信息框
 */
function showAutoDialog(timer, type, title) {
    Swal.fire({
        toast: true,
        position: 'center',
        background: '#000000',
        showConfirmButton: false,
        backdrop: false,
        timer : timer,
        type: type,
        title: title,
    });
}


/**
 * 验证邮件格式
 * @param mail
 * @returns {boolean}
 */
function validateEmail(mail) {
    var re = /^(([^<>()\[\]\.,;:\s@\"]+(\.[^<>()\[\]\.,;:\s@\"]+)*)|(\".+\"))@(([^<>()[\]\.,;:\s@\"]+\.)+[^<>()[\]\.,;:\s@\"]{2,})$/i;
    return re.test(mail);
}

/**
 * 用户共有的搜索方法，在很多页面的搜索框上都有
 */
function onSearchButtonClick() {
    var content = $('#input-search-content').val();
    if (content == undefined || content == '') {
        Swal.fire({
            type: 'error',
            title: '没输入要搜索的内容'
        });
        return;
    }
    window.location.href = encodeURI(document.location.origin + "/search?key=" + content)
}
